package ar.com.osdepym.agregacionafip.activities;

import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import ar.com.osdepym.agregacionafip.activities.context.AppContext;
import ar.com.osdepym.agregacionafip.adapters.CheckBoxListAdapter;
import ar.com.osdepym.agregacionafip.adapters.ContenedorCheckbox;
import ar.com.osdepym.agregacionafip.servicios.ServicioConfiguracion;

public class CuitsAgregadosActivity extends Activity implements OnClickListener {

	ListView listView;
	List<ContenedorCheckbox> contenedor = new Vector<ContenedorCheckbox>();
	ArrayAdapter<ContenedorCheckbox> adapter;
	private AppContext app;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		init();

		obtenerArchivos();
		onClickHandlers();
		fillListView();

	}

	private void init() {
		setContentView(R.layout.cuits_agregados);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
		this.app = (AppContext) this.getApplicationContext();
		

	}

	private void fillListView() {
		listView = (ListView) findViewById(R.id.cuits_agregados);

		adapter = new CheckBoxListAdapter(this, R.layout.checkboxlist_row,
				contenedor);

		View header = (View) getLayoutInflater().inflate(
				R.layout.listview_header, null);
		TextView t = (TextView) header.findViewById(R.id.listViewTitle);
		t.setText("Cuits a Agregar");

		listView.addHeaderView(header);
		// Assign adapter to ListView
		listView.setAdapter(adapter);

	}

	private void onClickHandlers() {
		Button b = (Button) findViewById(R.id.guardar_agregados);
		b.setOnClickListener(this);
	}

	private void obtenerArchivos() {
		
		
		try {
			contenedor.clear();
			contenedor = app.getConfiguracionAplicacion().getCuits();
			
			if (contenedor.size() <= 0) {
				Toast.makeText(this, "No existen Cuits", Toast.LENGTH_LONG)
						.show();
			}
		} catch (NullPointerException e) {
			Toast.makeText(this, "El directorio es inexistente",
					Toast.LENGTH_LONG).show();

		}

	}

	@Override
	public void onClick(View v) {
		ServicioConfiguracion s = new ServicioConfiguracion();
		s.guardarConfiguracion(this.app.getConfiguracionAplicacion());
		finish();
	}

}
